package com.qfedu.microcheck.rjx.check.service.impl;

import com.qfedu.common.core.entity.CheckRecord;
import com.qfedu.common.core.utils.UserUtils;
import com.qfedu.microcheck.rjx.check.mapper.CheckRecordMapper;
import com.qfedu.microcheck.rjx.check.service.CheckService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * ---just do it---
 *
 * @author ren
 * @date 2025/10/14
 * @desc
 */

@Service
public class CheckServiceImpl implements CheckService {
    @Resource
    private CheckRecordMapper checkRecordMapper;

    // 打卡签到或者签退
    @Override
    public void sign(Integer clockType) {
        // 创建打卡记录
        System.out.println("创建打卡记录");
        CheckRecord checkRecord = new CheckRecord();
        // 获取用户id
        Integer uid = UserUtils.getUid();
        // 测试---写死用户id
        uid = 1;
        // 获取打卡日期
        LocalDateTime checkDate = LocalDateTime.now();
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 打卡签到地点
        String checkAddress = "签到地点";
        // 打卡签到或者签退，根据当前时间判断是签到还是签退
        if (now.getHour() < 12) {
            // 签到
            checkRecord.setCheckType(clockType);
        } else {
            // 签退
            checkRecord.setCheckType(clockType);
        }
        checkRecord.setUserId(uid);
        checkRecord.setCheckDate(checkDate);
        checkRecord.setCheckTime(now);
        checkRecord.setCheckAddress(checkAddress);
        // 保存打卡记录--mybatis-plus
        checkRecordMapper.insert(checkRecord);

    }

    /**
     * ---just do it---
     * @desc: 查询打卡结果
     * @return 打卡结果
     */
    @Override
    public String checkResult(Integer clockType) {
        LocalDateTime now = LocalDateTime.now();
        //如果是1签到，2签退---9点之前签到，6点以后签退
        if (clockType == 1) {
            // 签到
            if (now.getHour() < 9) {
                return "签到成功";
            } else {
                return "迟到";
            }
        } else if (clockType == 2) {
            // 签退
            if (now.getHour() > 18) {
                return "签退成功";
            } else {
                return "早退";
            }
        }
        return null;
    }




}
